var api = require('../../utils/api.js');
var app = getApp();
Page({
    data: {
        chooseAudio: false,
        animationData: {},
        systemInfo: {},
        slidesImages:[],
        _api: {},
        list: [],
        total: 0,
        loadingMore: false,
        noMoreData: false,
        searchInputShowed: false,
        searchInputVal: '',
        searchingResult: false,
        searchKeyword: '',
        categoryList:[],
        albumList: [],
        audioList:[],
        audio_index:0,
        scrollInto: 0,
        currentzj:{},
        currentPageNumber: 1,
        isLogin:0
    },
    onLoad() {
        //确认登录
        this.checkLogin();
        //加载幻灯片
        this.loadSlidesImage();
        //加载所有专辑数据
        this.loadAlbumData();

        this.setData({
            _api: api,
        });
    },
    onShow() {

    },
    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
        wx.stopPullDownRefresh({
            success:()=>{
                //确认登录
                this.checkLogin();
                //重新加载幻灯片
                this.loadSlidesImage();
                //重新加载所有专辑数据
                this.loadAlbumData();
                console.log("刷新完成");
            }
        })
     return;
    },
    formatListItem(item) {
        if (item.Thumbnail) {
            item.Thumbnail = api.getUploadUrl(item.Thumbnail);
        }
        return item;
    },
    //点击图片触发事件
    showContent(e){
        // console.log(e)
        // console.log(e.currentTarget.id)
        // var id = e.currentTarget.id;
        // console.log(this.data);
        // this.setData({
        //     zjlist:this.data.list[id].more.files
        // });

        
    //    列表databind改变
        
    //    列表show

    },
    loadAlbumData(){
        var promise = (fun)=>{
            return new Promise((resolve,reject)=>{
                fun(resolve,reject);
            })}
        var p1 = promise((resolve,reject)=>{
            api.get({
                url:'portal/categories',
                data:{},
                success:data=>{
                    console.log(data);
                    this.setData({categoryList:data.data.list});
                    resolve();
                },
                fail:data=>{
                    reject();
                }
            })
        });
        var p2 = promise((resolve,reject)=>{
            api.get({
                url: 'portal/album',
                data: {
                    // order:'-published_time'
                },
                success: data => {
                    console.log(data);
                    var portalAlbumList = data.data.portalAlbumList;
                    var userRbacList = wx.getStorageSync("user").rbac||''
                    var finalPortalAlbumList = [];
                    for(var album in portalAlbumList){
                        var _rbac = (portalAlbumList[album].rbac||'').split(",");
                        for(var _r in _rbac){
                            //只要存在当前用户权限与专辑所要求权限交集
                            if(userRbacList.indexOf(_rbac[_r])!= -1){
                                finalPortalAlbumList.push(portalAlbumList[album]);
                                break;
                            }
                        }
                    }
                    this.setData({albumList:finalPortalAlbumList});
                    resolve();
                },
                fail:data=>{
                    reject();
                }
            });
        })
        Promise.all([p1,p2]).then(()=>{
            var list = this.data.categoryList;
            var zjlist = this.data.albumList;

            for(var i=0;i<list.length;i++){
                for(var n=0;n<zjlist.length;n++){
                    if(zjlist[n].cid == list[i].id){
                        console.log(list[i]);
                        if(list[i].albumList == null){
                            list[i].albumList = [];
                        }
                        list[i].albumList.push(zjlist[n]);
                    }
                }
            }
            this.setData({categoryList:list})
            console.log(this.data.categoryList);
        })
    },
    //加载幻灯片
    loadSlidesImage(){
        api.get({
            url:'home/slides/read',
            data:{'id':4},
            success:(data)=>{
                console.log(data);
                this.setData({slidesImages:data.data.items});
            },
            fail:(data)=>{
                wx.showToast({
                    'title':'获取幻灯片失败'
                })
            }
        })

    },
    checkLogin(){
        try {
            var isLogin = wx.getStorageSync('login');
            //未登录
            if (!isLogin == 1 ) {
                //显示界面登录元素
                this.setData({isLogin:0});
            }else{
                this.setData({isLogin:1});
            }

        } catch (e) {
            // Do something when catch error
        }
    },
    tapLoginBtn:function(){
        var promise = (fun)=>{
            return new Promise((resolve,reject)=>{
                fun(resolve,reject);
            })}
            var p1 = promise((resolve,reject)=>{
                api.login(resolve,reject);
            });
        var p2 = promise((resolve,reject)=>{
            //加载幻灯片
            this.loadSlidesImage();
            //加载所有专辑数据
            this.loadAlbumData();
            resolve();
        })
            Promise.all([p1,p2]).then(()=>{
                var isLogin = wx.getStorageSync('login');
                this.setData({isLogin:isLogin});
                wx.startPullDownRefresh();
                console.log("登录刷新完成");
            })
       ;


    },
    onShareAppMessage() {
        return {
            title: '喜欢听',
            path: '/pages/index'
        }
    },
    showModal: function (e) {
        var zjid = e.currentTarget.dataset.zjid;
        var showName = e.currentTarget.dataset.modal;
        var albumList = this.data.albumList;
        var currentzj={};
        var files = [];
        for(var i=0;i<albumList.length;i++){
            if(albumList[i].id == zjid){
                files = albumList[i].more?albumList[i].more.files:[];
                currentzj = albumList[i];
            }
        }
        for(var i in files){
            if(files[i].name != ''&& files[i].name.indexOf(".mp3")>0){
                var _name = files[i].name;
                files[i].name = _name.split('.mp3')[0];
            }
        }
        this.setData({
            modalName: showName,
            currentzj:currentzj,
            audioList:files
        })
    },
    closeModal: function (e) {
        this.setData({
            modalName: null
        })
    },
    play:function(e){
        var audio_index = e.currentTarget.dataset.id;
        this.closeModal(e);
        wx.setStorageSync("album",{
                "albumInfo": this.data.currentzj,
                "audiolist":this.data.audioList,
                "curAudioIndex":audio_index
        });
        //    跳转tabbar页面
        wx.switchTab({
            url: '/pages/play/play',
            success: function(e){
                console.log("跳转完成");
            }
        });

    },
    inputConfirm:function(e){
        if((e.detail.value||'').trim() != ''){
            wx.navigateTo({
                url:'/pages/search/search?keyword='+e.detail.value,
                success:()=>{

                }
            })
        }

    }

});